package com.yingside.jdbc.crm;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoRadioButton;
import com.lovo.netCRM.component.LovoTxt;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmployeeAddFrame extends JFrame {
    private LovoTxt txtName,txtTel;
    private LovoRadioButton radioSex;
    private LovoComboBox cbbDept;
    private LovoButton btnAdd,btnCanel;
    private EmployeeDao employeeDao = new EmployeeDao();
    private DeptDao deptDao = new DeptDao();
    private EmployeeFrame employeeFrame;
    public EmployeeAddFrame(EmployeeFrame employeeFrame) {
        this.employeeFrame = employeeFrame;
        this.setTitle("员工新增");
        this.setBounds(300,200,400,400);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.setLayout(null);
        initComponent();
    }

    private void initComponent() {
        txtName = new LovoTxt("员工姓名:", 40,80,this);
        txtTel = new LovoTxt("员工电话:", 40,130,this);
        radioSex = new LovoRadioButton("性别",
                new String[]{"男","女"},
                40,180,
                this);

        List<Dept> list = deptDao.list();
        cbbDept = new LovoComboBox("部门",list,40,230,this);

        btnAdd = new LovoButton("新增",40,280,this);
        btnCanel = new LovoButton("取消",140,280,this);

        btnAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = txtName.getText();
                if(name == null || "".equals(name)){
                    JOptionPane.showMessageDialog(null,"员工姓名不能为空");
                    txtName.grabFocus();
                    return;
                }

                String tel = txtTel.getText();
                if(tel == null || "".equals(tel)){
                    JOptionPane.showMessageDialog(null,"员工电话不能为空");
                    txtTel.grabFocus();
                    return;
                }
                String regex = "13\\d{8}";
                //编译正则表达式字符串
                Pattern pattern = Pattern.compile(regex);
                //获取字符串匹配对象
                Matcher matcher = pattern.matcher(tel);
                //匹配
                if(!matcher.matches()){
                    JOptionPane.showMessageDialog(null,"电话号码格式不正确");
                    txtTel.grabFocus();
                    return;
                }

                String sex = radioSex.getItem();

                Dept dept = (Dept)cbbDept.getItem();

                Employee employee = new Employee();
                employee.setEmpName(name);
                employee.setEmpTel(tel);
                employee.setEmpSex(sex);
                employee.setDept(dept);

                int n = employeeDao.add(employee);

                if(n > 0){
                    JOptionPane.showMessageDialog(null,"新增成功");
                    employeeFrame.initData();
                }else{
                    JOptionPane.showMessageDialog(null,"新增失败");
                }
            }
        });

    }
}
